/*
 *  Class name: EditablePlayerListItem.java
 *  Version: 1.0
 *  Date: 11.12.2013
 */
package cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.items.entity;

import cz.muni.fi.pa165.soccergamesarchive.businesslayer.dtos.PlayerDTO;
import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.PageableListPanel;
import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.entity.EditablePlayerList;
import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.items.PageableListItemButton;
import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.dialogs.PlayerDetailDialogWindow;
import java.awt.Dialog;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import static cz.muni.fi.pa165.soccergamesarchive.restclient.utils.JOptionDialogWindowHandler.showYesNoDialogWindow;

/**
 *
 * @author Michal Ďuriš
 */
public class EditablePlayerListItem extends PageableListItemButton<PlayerDTO> {

    private static final long serialVersionUID = 1L;

    public EditablePlayerListItem(PageableListPanel<PlayerDTO> parentList,
                                  PlayerDTO entity) {
        super(parentList, entity);
    }

    @Override
    public void init() {
        setIcon(new ImageIcon(getClass().getResource(
                "/icons/components/footballer-icon.png")));
    }

    @Override
    public void setEntity(PlayerDTO entity) {
        this.entity.setId(entity.getId());
        this.entity.setFirstName(entity.getFirstName());
        this.entity.setLastName(entity.getLastName());
        this.entity.setShirtNumber(entity.getShirtNumber());
        this.entity.setTeam(entity.getTeam());
    }

    @Override
    public String getEntityText() {
        return "<html> First name: " + entity.getFirstName()
                + "<br> Last name: " + entity.getLastName()
                + "<br> Shirtnumber: " + entity.getShirtNumber();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {

            //TODO: Rethink this
            new PlayerDetailDialogWindow(
                    ((EditablePlayerList) getParentList()).getParentEntityItem(),
                    this,
                    Dialog.ModalityType.APPLICATION_MODAL).init().setVisible(
                            true);

        } else if (e.getButton() == MouseEvent.BUTTON3) {

            if (showYesNoDialogWindow(parentList.getOwner(),
                    "Remove player",
                    "Do you really want to delete this player ?")
                    == JOptionPane.YES_OPTION) {
                parentList.removeListItem(this, null);
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

}
